package com.mall.service.homepage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mall.commons.common.CommonPage;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.SmsHomeBrand;
import com.mall.service.homepage.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/home/brand")
public class BrandController {


    @Autowired
    private BrandService brandService;

    /**
     * 添加首页推荐品牌
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody List<SmsHomeBrand> smsHomeBrand){
        brandService.saveBatch(smsHomeBrand);
        return CommonResult.success("添加成功");
    }
    /**
     * 批量删除推荐品牌
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestParam List<Long> ids){
         brandService.removeByIds(ids);
         return CommonResult.success("删除成功");
    }
    /**
     * 分页查询推荐品牌String brandName,
     */
    @GetMapping("/list")
    public CommonResult<CommonPage<SmsHomeBrand>> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                                       @RequestParam(defaultValue = "10") Integer pageSize,String brandName,Integer recommendStatus){
         CommonPage<SmsHomeBrand> page =brandService.findByPage(pageNum,pageSize,brandName,recommendStatus);

        return CommonResult.success(page);
    }
    /**
     * 批量修改推荐品牌状态
     */
    @PostMapping("/update/recommendStatus")
    public CommonResult updateRecommendStatus(@RequestParam List<Long> ids ,Integer recommendStatus){
        LambdaQueryWrapper<SmsHomeBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SmsHomeBrand::getId ,ids);
        List<SmsHomeBrand> list = brandService.list(queryWrapper);
        ArrayList<SmsHomeBrand> objects = new ArrayList<>();
        for (SmsHomeBrand smsHomeBrand : list) {
            smsHomeBrand.setRecommendStatus(recommendStatus);
            objects.add(smsHomeBrand);
        }

        brandService.updateBatchById(objects);
        return CommonResult.success("批量修改成功");
    }
    /**
     * 修改推荐品牌排序
     */
    @PostMapping("/update/sort/{id}")
    public CommonResult updateSort(@PathVariable Long id,Integer sort){
        SmsHomeBrand smsHomeBrand = new SmsHomeBrand();
        smsHomeBrand.setId(id);
        smsHomeBrand.setSort(sort);
        brandService.updateById(smsHomeBrand);
        return CommonResult.success("修改成功");
    }
}
